var hepler = {
    addCart(data) {
        //加入购物车
        /**
         * 判断本地有没有数据
         */
        console.log(data)
        var cartArray = [];
        var shopList = wx.getStorageSync('shopList');
        console.log('shopList',shopList)
        if (shopList) {
            var carArray = JSON.parse(shopList);
            if(this.cartHasData(carArray,data)){
                for (let i = 0; i < carArray.length; i++) {
                    if(carArray[i]._id == data.id){
                        carArray[i].num = carArray[i].num + 1;
                    }
                }
                console.log(1111111,carArray)
                wx.setStorageSync('shopList', JSON.stringify(carArray));
            }else{
                cartArray.push(data);
                console.log(22222,cartArray)
                wx.setStorageSync('shopList', JSON.stringify(cartArray));
            }

        } else {
            var cartArray = JSON.parse(shopList);
            //没有的操作
            data.num = 1;
            cartArray.push(data)
            console.log(333333,cartArray)

            wx.setStorageSync('shopList',JSON.stringify(cartArray));
        }

    },
    cartHasData(carArray,data){
        for (let i = 0; i < carArray.length; i++) {
            let ID = carArray[i]._id;
            if(ID === data._id) return true;
        }
        return false;
    }
}




module.exports = hepler;












